package QRCG;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/ViewTheGameServe")
public class ViewTheGameServe extends HttpServlet {
	private static final long serialVersionUID = -2837280410806047003L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		String gameId = request.getParameter("game_id");
		String nickname = (String) session.getAttribute("nickname");
		String message = "";

		String activeValue = request.getParameter("set_active");
		
		boolean isVoting = Util.hasTextContent(request.getParameter("vote"));
		boolean isRegistering = Util.hasTextContent(request.getParameter("register"));
		boolean viewingQR = Util.hasTextContent(request.getParameter("view_qr"));
		boolean settingActive = Util.hasTextContent(activeValue);

		if (viewingQR) {
			
			RequestDispatcher rd = request.getRequestDispatcher("GameCreated.jsp?game_id=" + gameId);
			
		    String[] hintDescriptions = request.getParameterValues("hint_descriptions");
		    String[] hintKeywords = request.getParameterValues("hint_keywords");
		    String[] hintCoordinates = request.getParameterValues("hint_coordinates");
		    String[] hintAddresses = request.getParameterValues("hint_addresses");
			
		    request.setAttribute("hint_descriptions", hintDescriptions);
		    request.setAttribute("hint_keywords", hintKeywords);
		    request.setAttribute("hint_coordinates", hintCoordinates);
		    request.setAttribute("hint_addresses", hintAddresses);
		    request.setAttribute("game_id", gameId);

			rd.forward(request, response);
			
		} else {

			String rating = request.getParameter("stars");

			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String now = df.format(new Date());
			System.out.println(now);

			DatabaseManager dm = new DatabaseManager();
			boolean success = true;
			try {
				dm.connect();
				if (isRegistering) {
					// register button pressed
					success = dm.registerToGame(nickname, gameId, now);

				} else if (isVoting) {
					// vote button pressed

					rating = request.getParameter("stars");
					success = dm.rateGame(nickname, gameId, rating);
				} else if(settingActive){
					// activate/deactivate button pressed
					String active = activeValue.equalsIgnoreCase("Activate") ? "true" : "false";
					success = dm.setGameActive(active, nickname, gameId);
				}
			} catch (SQLException e) {
				e.printStackTrace();
				success = false;
			}

			if (success) {
				if (isRegistering) {
					message = "You are now registered to the game!";
				} else if (isVoting) {
					message = "Thank you for voting!";
				} else if (settingActive) {
					String activeMessage = "activated";
					if(activeValue.equalsIgnoreCase("Deactivate")){
						activeMessage = "deactivated";
					}
					message = "The game is now"+ activeMessage + ".";
				}
			} else {
				if (isRegistering) {
					message = "There was a problem and we couldn't register you to the game! Please try again or contact admin.";
				} else if (isVoting) {
					message = "There was a problem and we couldn't record your vote! Please try again or contact admin.";
				} else if (settingActive) {
					message = "There was a problem and we couldn't activate/deactivate your game! Please try again or contact admin.";
				}
			}

			RequestDispatcher rd = request
					.getRequestDispatcher("ViewTheGame.jsp?game_id=" + gameId);

			request.setAttribute("message", message);
			rd.forward(request, response);
		}
	}
}
